package com.yunyouyi.grabtickets.config;

import com.yunyouyi.grabtickets.entity.Ticket;
import com.yunyouyi.grabtickets.mapper.TicketMapper;
import com.yunyouyi.grabtickets.redis.RedisService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class StockInitializer implements CommandLineRunner {

    @Resource
    private TicketMapper ticketMapper;

    @Resource
    private RedisService redisService;

    @Override
    public void run(String... args) throws Exception {
        List<Ticket> tickets = ticketMapper.selectList(null);
        for (Ticket ticket : tickets) {
            redisService.setStock("stock:" + ticket.getTrainId(), ticket.getStock());
        }
    }
}